package com.yue.yadmin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.yue.yadmin.service.SysMenuService;
import com.yue.yadmin.utils.Log;
import com.yue.yadmin.utils.Result;
import com.yue.yadmin.utils.ShiroUtils;

@Controller
public class SysIndexController {
	@Autowired
	SysMenuService sysMenuService;
	@RequestMapping("/main")
	public ModelAndView index(){
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Log.getLogger(this).info("req:{}",req);
		ModelAndView modelAndView = new ModelAndView("index");
		modelAndView.addObject("menu",sysMenuService.getCurrentUserMenuList());
		return modelAndView;
	}
	
	@RequestMapping("/toLogin")
	public String login(){
		return "login";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletResponse response){
		ShiroUtils.logout();
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
		response.setDateHeader("Expires", 0); // Proxies.
		return "login";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Result login(String userName, String password){
		if(StringUtils.isBlank(userName))return Result.error("用户名不能为空");
		if(StringUtils.isBlank(password))return Result.error("密码不能为空");
		return ShiroUtils.login(userName,password);
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Result register(String userName,String password,String password2){
		
		return null;
	}
}
